package jp.co.sharp.android.parents.kidsguard.activity.setting.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import jp.co.sharp.android.parents.kidsguard.R;

public class OkaeriNotificationDlg extends Dialog {

    private OkaeriNameSetListener okaeriNameSetListener;
    private String name;
    private String ID;
    private String deviceName;
    private OkaeriNotificationDlg okaeriNotificationDialog;

    public OkaeriNotificationDlg(@NonNull Context context, String ID, String deviceName) {
        super(context);
        okaeriNotificationDialog = this;
        this.ID = ID;
        this.deviceName = deviceName;
    }

    public OkaeriNotificationDlg(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        okaeriNotificationDialog = this;
    }

    protected OkaeriNotificationDlg(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        okaeriNotificationDialog = this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okaeri_notification_dialog);

        editTextChange();
        setID();
        setEditTextDeviceName();

        Button buttonOK = (Button) this.findViewById(R.id.yes);
        buttonOK.setOnClickListener(new OkaeriNotificationDlg.ListenerOK());

        Button buttonCancel = (Button) this.findViewById(R.id.cancel);
        buttonCancel.setOnClickListener(new OkaeriNotificationDlg.ListenerCancel());
    }


    /**
     *
     */
    public interface OkaeriNameSetListener {
        void OkaeriNameSet(String name);
    }

    /*
     * 对外公开方法让Activity实现
     */
    public void setOkaeriNameSetListener(OkaeriNotificationDlg.OkaeriNameSetListener callBack) {
        okaeriNameSetListener = callBack;
    }

    class ListenerOK implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (okaeriNameSetListener != null) {
                okaeriNameSetListener.OkaeriNameSet(name);
                OkaeriNotificationDlg.this.dismiss();
            }
        }
    }

    class ListenerCancel implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            if (okaeriNameSetListener != null) {
                OkaeriNotificationDlg.this.dismiss();
            }
        }
    }

    public void editTextChange() {
        EditText editText = findViewById(R.id.editText);
        // 对EditText内容的实时监听
        editText.addTextChangedListener(new TextWatcher() {
            // 输入文本之前的状态
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//                System.out.println("s"+s.toString());
            }

            // 输入文本中的状态
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            // 输入文本之后的状态
            @Override
            public void afterTextChanged(Editable s) {
                name = s.toString();
            }
        });

        editText.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                Drawable drawable = editText.getCompoundDrawables()[2];
                if (drawable == null) {
                    return false;
                }
                if (event.getAction() != MotionEvent.ACTION_UP) {
                    return false;
                }
                if (event.getX() > editText.getWidth()
                        - editText.getPaddingRight()
                        - drawable.getIntrinsicWidth()) {
                    editText.setText("");
                }
                return false;
            }

        });
    }

    public void setID() {
        TextView textView = findViewById(R.id.ID);
        textView.setText(ID);
    }

    public void setEditTextDeviceName() {
        TextView textView = findViewById(R.id.editText);
        textView.setText(deviceName);
    }

}
